package views.games;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

import views.util.GameColors;
import core.constants.Constants;

public class ColoriEParoleView extends GameView implements ActionListener {
	private Timer cronometro = new Timer(1000, this);
	private int timeMax = Constants.getGameTime();
	private MainPanel main;
	private JPanel inputPanel;
	private GridBagConstraints gbc = new GridBagConstraints();
	private JLabel word;
	private JLabel meaning;
	private JButton inputYes = new JButton("SI");
	private JButton inputNo = new JButton("NO");

	private Color[] currentColor; // colore con cui è scritta la parola
	private String[] currentMeaning; // già  pronto per il gioco
	private int numColor;
	private int gameIndex = 0;
	private ActionListener currentActionListener;
	private KeyListener currentKeyListener;

	public ColoriEParoleView(String firstWord, String[] currentMeaning,
			String[] currentRandomizedColor) {
		super(GameColors.BORDEAUX, GameColors.BORDEAUX_CHIARO, "1:00",
				"PUNTEGGIO: 0", "CONFRONTO 1");
		this.currentMeaning = currentMeaning;
		numColor = currentMeaning.length;
		buildArrayColor(currentRandomizedColor);
		main = new MainPanel(new GridBagLayout());
		main.setBackground(GameColors.BORDEAUX);
		inputPanel = new JPanel(new GridLayout(1, 2, 2, 2));
		add(main, BorderLayout.CENTER);
		word = new JLabel(firstWord.toLowerCase(), JLabel.CENTER);
		word.setFont(new Font("Segoe UI", Font.BOLD, 45));
		word.setForeground(currentColor[gameIndex]);
		meaning = new JLabel(currentMeaning[gameIndex].toLowerCase(),
				JLabel.CENTER);
		meaning.setFont(new Font("Segoe UI", Font.BOLD, 45));
		meaning.setForeground(Color.BLACK);

		gbc.gridx = 0;
		gbc.gridy = 0;
		gbc.insets = new Insets(15, 10, 20, 10);
		main.add(word, gbc);
		gbc.gridy = 1;
		gbc.insets = new Insets(0, 10, 50, 10);
		main.add(meaning, gbc);
		gbc.gridy = 2;
		main.add(inputPanel, gbc);

		inputPanel.add(inputNo);
		inputPanel.add(inputYes);
		inputPanel.setPreferredSize(new Dimension(180, 40));
		inputPanel.setBackground(GameColors.BORDEAUX);

		cronometro.start();
	}

	public void checkFocusMain() {
		main.requestFocus();
	}

	// Colori possibili: NERO, BLU, GIALLO, ROSSO, VERDE
	private void buildArrayColor(String[] currentRandomizedColor) {
		currentColor = new Color[numColor];
		for (int i = 0; i < numColor; i++) {
			switch (currentRandomizedColor[i]) {
			case "NERO":
				currentColor[i] = Color.BLACK;
				break;
			case "BLU":
				currentColor[i] = Color.BLUE;
				break;
			case "GIALLO":
				currentColor[i] = new Color(230, 230, 0);
				break;
			case "ROSSO":
				currentColor[i] = Color.RED;
				break;
			default:
				currentColor[i] = new Color(54, 196, 19);
			}
		}
	}

	public int getGameIndex() {
		return gameIndex;
	}

	public void refresh(String currentWord, int gameCount) {
		gameIndex = (gameIndex + 1) % numColor;
		word.setText(currentWord);
		word.setForeground(currentColor[gameIndex]);
		meaning.setText(currentMeaning[gameIndex]);
		meaning.setForeground(Color.BLACK);
		other.setText("CONFRONTO " + gameCount);
	}

	@Override
	public void gameEnded() {
		other.setVisible(false);
		gameClock.setVisible(false);
		main.removeAll();
		main.deleteAll();
		JLabel end = newLabel("Partita Conclusa!", GameColors.BORDEAUX_CHIARO);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 4;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(0, 0, 0, 0);
		main.add(end, gbc);
		gbc.gridy = 1;
		main.add(score, gbc);
		JButton restart = newButton("Ricomincia");
		JButton menu = newButton("Menu Giochi");
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		main.add(restart, gbc);
		gbc.gridx = 2;
		main.add(menu, gbc);
		restart.addActionListener(currentActionListener);
		menu.addActionListener(currentActionListener);
		main.removeKeyListener(currentKeyListener);
		updateUI();
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		timeMax--;
		gameClock.setText("0:" + timeMax);
		if (timeMax == 0)
			gameEnded();

	}

	public void addActionListenerToGame(ActionListener al) {
		currentActionListener = al;
		inputYes.addActionListener(al);
		inputNo.addActionListener(al);
	}

	public void addKeyListenerToGame(KeyListener kl) {
		currentKeyListener = kl;
		main.addKeyListener(kl);
	}

	public void setScore(int newScore) {
		score.setText("PUNTEGGIO: " + newScore);

	}

	public void randomize(String firstWord, String[] currentMeaning,String[] currentRandomizedColor) {
		word.setText(firstWord);
		this.currentMeaning = currentMeaning;
		buildArrayColor(currentRandomizedColor);

	}


	@SuppressWarnings("serial")
	private class MainPanel extends JPanel {
		private boolean end;

		public MainPanel(LayoutManager lm) {
			super(lm);
		}

		public void paintComponent(Graphics g) {

			super.paintComponent(g);
			if (!end) {
				g.setColor(Color.WHITE);
				g.fillRoundRect(300, 160, 200, 65, 20, 20);
				g.fillRoundRect(300, 240, 200, 65, 20, 20);
			}
		}

		public void deleteAll() {
			end = true;
			repaint();
		}

	}

}
